public class Resource {
    private int resource;
    private boolean state = false; // 表示有资源
    public synchronized void produce(int resource)  {
        if(state){   //如果缓存区非空，则等待
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
           //如果缓存区为空，则生产资源
            this.resource = resource;
        System.out.println("Produce resource :" + this.resource);
        state = true;
        notifyAll();   //唤醒所有等待线程
    }
    public synchronized void consume(){
        if(!state){    //如果缓存区为空，则等待
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
         //否则进行消费
        System.out.println("Consume resource :" + this.resource);
        state = false;
        notifyAll();
    }
}
